<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="气体侦测器类型:" label-width="140px" prop="gasType">
        <el-select v-model="queryParams.gasType" placeholder="请选择气体侦测器类型">
          <el-option
            v-for="dict in gasTypeOption"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所在部门:" label-width="140px" prop="deptId">
        <el-tree-select
          v-model="queryParams.deptId"
          :data="deptOptions"
          :props="{ value: 'id', label: 'label', children: 'children' }"
          value-key="id"
          placeholder="请选择部门"
          check-strictly
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>
    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column
        label="侦测器名称"
        width="260"
        :show-overflow-tooltip="true"
        prop="gasName"
      />
      <el-table-column
        label="所属部门"
        width="90"
        :show-overflow-tooltip="true"
        prop="deptName"
      >
        <template #default="scope">
          <span>{{ scope.row.deptName ? scope.row.deptName : "---" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备编号" prop="gasCode" />
      <el-table-column label="设备类型" width="140" prop="gasType">
        <template #default="scope">
          <span>{{ scope.row.gasType ? parseGasType(scope.row.gasType) : "---" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="参数" width="80" prop="parameterName" />
      <el-table-column label="单位" width="80" prop="units" />
      <el-table-column
        label="位号"
        prop="bitNumber"
        width="180"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="第一报警值"
        prop="firstStageAlarm"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="第二报警值"
        prop="secondStageAlarm"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="实时值"
        prop="realTimeValue"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="采集时间" prop="realTimeAt" width="180">
        <template #default="scope">
          <span>{{
            scope.row.realTimeAt ? parseTime(scope.row.realTimeAt) : "---"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="primary"
              icon="Delete"
              @click="handleDelete(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
    <!--新增或者修改气体侦测及储罐设备对话框-->
    <el-dialog :title="title" v-model="open" width="60%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item label="气体侦测器类型:" label-width="140px" prop="gasType">
          <el-select v-model="form.gasType" placeholder="请选择气体侦测器类型">
            <el-option
              v-for="dict in gasTypeOption"
              :key="dict.id"
              :label="dict.label"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="气体侦测器编号:" label-width="140px" prop="gasCode">
          <el-input v-model="form.gasCode" placeholder="请输入气体侦测器编号" />
        </el-form-item>
        <el-form-item label="气体侦测器名称:" label-width="140px" prop="gasName">
          <el-input v-model="form.gasName" placeholder="请输入气体侦测器名称" />
        </el-form-item>
        <el-form-item label="气体侦测器位号:" label-width="140px" prop="bitNumber">
          <el-input v-model="form.bitNumber" placeholder="请输入气体侦测器位号" />
        </el-form-item>
        <el-form-item label="所在部门:" label-width="140px" prop="deptId">
          <el-tree-select
            v-model="form.deptId"
            :data="deptOptions"
            :props="{ value: 'id', label: 'label', children: 'children' }"
            value-key="id"
            placeholder="请选择部门"
            check-strictly
          />
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="参数名称:" label-width="140px" prop="parameterName">
              <el-input v-model="form.parameterName" placeholder="请输入监测参数名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参数单位:" label-width="140px" prop="units">
              <el-input v-model="form.units" placeholder="请输入监测参数单位" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="第一报警值:" label-width="140px" prop="firstStageAlarm">
              <el-input v-model="form.firstStageAlarm" placeholder="请输入第一报警值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第二报警值:" label-width="140px" prop="secondStageAlarm">
              <el-input v-model="form.secondStageAlarm" placeholder="请输入第二报警值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="参数报警规则:" label-width="140px" prop="alarmType">
          <el-select v-model="form.alarmType" placeholder="请选择所属监测点报警规则">
            <el-option
              v-for="dict in rulesOptions"
              :key="dict.code"
              :label="dict.value"
              :value="dict.code"
            />
          </el-select>
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="所在位置经度:" label-width="140px" prop="latitude">
              <el-input
                v-model="form.latitude"
                placeholder="请输入气体侦测器所在位置经度"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在位置纬度:" label-width="140px" prop="longitude">
              <el-input
                v-model="form.longitude"
                placeholder="请输入气体侦测器所在位置纬度"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="三维地图坐标:" label-width="140px" prop="coord">
          <el-input v-model="form.coord" placeholder="请输入设备所在三维地图坐标" />
        </el-form-item>
        <el-row :gutter="12">
          <el-col :span="12">
            <el-form-item label="量程最小值:" label-width="140px" prop="minimumRange">
              <el-input v-model="form.minimumRange" placeholder="请输入量程最小值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="量程最大值:" label-width="140px" prop="maximumRange">
              <el-input v-model="form.maximumRange" placeholder="请输入量程最大值" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="ScreenGas">
import { listGas, addGas, deleteGas, updateGas, getGas } from "@/api/screen/gas";
import { getMonitorRule } from "@/api/screen/tank";
import { deptTreeSelect } from "@/api/system/user";
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const title = ref("");
const open = ref(false);
const { proxy } = getCurrentInstance();
const deptOptions = ref([]);
const gasTypeOption = ref([
  { id: "1", label: "可燃气体侦测器" },
  { id: "2", label: "有毒气体侦测器" },
  { id: "3", label: "其他类型" },
]);
const rulesOptions = ref([]);
const data = reactive({
  form: {
    typeId: undefined,
  },
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
  },
  rules: {
    gasName: [
      {
        required: true,
        message: "气体侦测器名称必须填写！",
        trigger: ["blur", "change"],
      },
      {
        min: 3,
        max: 100,
        message: "名称必须介于 3 和 100 之间",
        trigger: "blur",
      },
    ],
    gasCode: [
      {
        required: true,
        message: "气体侦测器编号必须填写！",
        trigger: ["blur", "change"],
      },
      {
        min: 3,
        max: 100,
        message: "编号必须介于 3 和 100 之间",
        trigger: "blur",
      },
    ],
    gasType: [{ required: true, message: "气体侦测器类型必须选择！", trigger: "blur" }],
    bitNumber: [{ required: true, message: "设备位号必须填写！", trigger: "blur" }],
    deptId: [{ required: true, message: "部门必须选择！", trigger: "blur" }],
    units: [{ required: true, message: "参数单位必须填写！", trigger: "blur" }],
    coord: [{ required: true, message: "三维坐标必须填写！", trigger: "blur" }],
    parameterType: [
      { required: true, message: "监测点参数类型必须选择！", trigger: "blur" },
    ],
    firstStageAlarm: [
      { required: true, message: "第一报警值必须填写！", trigger: "blur" },
    ],
    secondStageAlarm: [
      { required: true, message: "第二报警值必须填写！", trigger: "blur" },
    ],
    alarmType: [{ required: true, message: "报警规则必须选择", trigger: "blur" }],
  },
});
const { queryParams, form, rules } = toRefs(data);
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}
/** 获取监测点数据报警规则数据列表 */
function getRuleList() {
  getMonitorRule().then((res) => {
    rulesOptions.value = res.data;
    console.log(res);
  });
}
/** 解析气体侦测器类型 */
function parseGasType(val) {
  var itemTarget = gasTypeOption.value.filter((item) => {
    return item.id == val;
  });

  return itemTarget[0].label;
}
/** 获取列表数据 */
function getList() {
  loading.value = true;
  listGas(queryParams.value).then((response) => {
    loading.value = false;
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
  });
}
/** 重置 */
function reset() {
  form.value = {};
  proxy.resetForm("formRef");
}

/** 处理新增 */
function handleAdd() {
  open.value = true;
  title.value = "新增气体侦测器";
  reset();
}

/** 处理删除 */
function handleDelete(row) {
  proxy.$modal
    .confirm('是否确认删除名称为"' + row.gasName + '"的数据项?')
    .then(function () {
      return deleteGas(row.gasId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 处理编辑 */
function handleUpdate(row) {
  getGas(row.gasId).then((response) => {
    open.value = true;
    title.value = "编辑气体侦测器设备";
    form.value = response.data;
  });
}
/** 处理取消 */
function cancel() {
  open.value = false;
}

/** 处理数据提交 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.gasId != undefined) {
        updateGas(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改气体侦测器成功");
          open.value = false;
          getList();
        });
      } else {
        addGas(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增气体侦测器成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
getRuleList();
getList();
getDeptTree();
</script>
